<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<style type="text/css">
		#jianju{
			padding-left:70px
		}
		
	</style>
</head>
<body>
 <script type="text/javascript">
 var tt;
 var price;
 var ss=0;
$(function(){
 //打开药品列表
 		$("#drug-add").linkbutton({
 			onClick:function(){
 				$("#drugs").window("open");
 				$("#mxjcpname").textbox("reset");
 				ses()
 				$("#Mxjtbdrugs").datagrid("reload");
 			}
 		})
 		//加载手术ID
 		$("#ssNo").combogrid({
		onOpen:function(){
			getnewid()
		}
	})
 //双击添加药品 
 	$("#Mxjtbdrugs").datagrid({
 		onDblClickRow:function(index,row){
 			$("#drug").datagrid("appendRow",{
 				'cpName': [row.cpName],
				'cpType': [row.cpType],
				'yfxqUnit': [row.yfxqUnit],
				'ygxqNumber': [1],
				'ygxqSaleprice': [row.ygxqSaleprice],
				'cpFunction': [row.cpFunction]
 			});
 			$("#Mxjtbdrugs").datagrid("deleteRow",index)//从列表移除已添加的药品
 		}
 	  });
 //药品分类查询
 	$("#mxjcptype").combobox({
 		onChange:function(newValue,oldValue){
 			$('#Mxjtbdrugs').datagrid({
				url : 'ATypeDrug',
			method : 'post',
			queryParams : {
				cpType:newValue
			}
		})
 		}
 	})
 	//回车搜索
		$(document).keydown(function(e){ 
			if(e.keyCode == 13){ 
				ses()
			}
		});
 	//按钮药品搜索
 	$("#mxjcpname").textbox({
 		onClickButton:function(){
 			var value=$("#mxjcpname").textbox("getValue")
 			$('#Mxjtbdrugs').datagrid({
 			url : 'DrugsName',
 		method : 'post',
 		queryParams : {
 			cpName:value
 		}
 	})
 		}
 	})
  
 	//提交手术药品
			$("#drugtj").linkbutton({
				onClick : function() {
					var rows = $("#drug").datagrid("getRows")
					var aa = [];
					for (var i = 0; i < rows.length; i++) {
						aa.push(rows[i].cpName)
						aa.push(rows[i].cpType)
						aa.push(rows[i].yfxqUnit)
						aa.push(rows[i].ygxqNumber)
						aa.push(rows[i].ygxqSaleprice)
						aa.push(rows[i].cpFunction)
					}

					$("#mxjdrugform").form('submit',{
						url:'mxjSavaDrug',
						onSubmit: function(param){
					    	param.arr=aa
					    },
						success:function(data){
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
						    $("#mxjdrugform").form("clear");
						}
					})
					//删除药品已添加的药品
					for (var i = 0; i <= rows.length; i++) {
						$("#drug").datagrid("deleteRow",0);
					}
				}
			})
			
		//单价
	$("#drug").datagrid({
		onClickRow:function(index,data){
			price=data.ygxqSaleprice/data.ygxqNumber
		}
	})
	
	//根据药品数量变更价格
	$("#drug").datagrid({
				onAfterEdit:function(rowIndex, rowData, changes){
					var as=rowData.ygxqNumber;
					var c=price*as;
					$('#drug').datagrid('updateRow',{
						index: rowIndex,
						row: {
							ygxqSaleprice: c
						}
					});
				}
			})
 	
 	})
 	
 	
var editIndex = undefined;
//结束正在编辑的行
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#drug').datagrid('validateRow', editIndex)){
		$('#drug').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
//药品搜索
function ses(){
	var value=$("#mxjcpname").textbox("getValue")
		$('#Mxjtbdrugs').datagrid({
		url : 'DrugsName',
	method : 'post',
	queryParams : {
		cpName:value
	}
})
}
//事件：单击单元格的事件
function onClickCell(index, field){
	if (editIndex != index){
		if (endEditing()){
			$('#drug').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			var ed = $('#drug').datagrid('getEditor', {index:index,field:field});
			if (ed){
				($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
			}
			editIndex = index;
		} else {
			setTimeout(function(){
				$('#drug').datagrid('selectRow', editIndex);
			},0);
		}
	}
}


//删除行
function removeit(){
	if (editIndex == undefined){return}
	$('#drug').datagrid('cancelEdit', editIndex)
			.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}

//应用
function accept(){
	if (endEditing()){
		endEditing();
	}
}
//撤消
function reject(){
	$('#drug').datagrid('rejectChanges');
	editIndex = undefined;
}
//获取数据
function getChanges(){
	var rows = $('#drug').datagrid('getChanges');
	alert(rows.length+' rows are changed!');
}
//结束编辑，同步数据
function onEndEdit(index, row){
	var ed = $(this).datagrid('getEditor', {
		index: index,
		field: 'ygxqSaleprice'
	});
	row.ygxqSaleprice = $(ed.target).combobox('getText');
}
//获取新添加的手术id
function getnewid(){
	$.getJSON('Aopid',
		function(data){
			$("#ssNo").combogrid("setValue",data);
	})
}
</script>
		<form id="mxjdrugform">
		<div style="padding-top:10px;padding-left:30px">
		<select id="ssNo" class="easyui-combogrid" name="ssNo"
					data-options="label:'手术名称',
																							width:'40%', 
																							labelWidth:'25%',
																							panelWidth: '20%',
																							url:'opdrug',
																							panelHeight:'auto',
																							idField: 'ssNo',
																							textField: 'ssName',
																							columns:[[
																								{field:'ylkNo',title:'医疗卡号',width:95},
																				                {field:'brName',title:'病人姓名',width:95},
																				                {field:'ssName',title:'手术名称',width:95},
																				                {field:'brSex',title:'性别',width:95},
																				                {field:'brAge',title:'年龄',width:95},
																				                {field:'ksName',title:'科室',width:95}
																				            ]]
																							"></select></div>
		
		<div style="padding:10px">
        	<table id="drug" class="easyui-datagrid"style="height:auto"
							data-options="
								iconCls: 'icon-edit',
								singleSelect: true,
								toolbar: '#tbdrug',
								method: 'get',
								onClickCell: onClickCell,
								onEndEdit: onEndEdit,
								width: '100%'
							">
						<thead>
							<tr>
								<th data-options="field:'cpName',width:'20%',editor:'textbox'">药物名称</th>
								<th data-options="field:'cpType',width:'10%',editor:'textbox'">类型</th>
								<th data-options="field:'yfxqUnit',width:'10%',editor:'textbox'">单位</th>
								<th data-options="field:'ygxqNumber',width:'10%',editor:{type:'numberspinner',options:{min:1}} ">数量</th>
								<th data-options="field:'ygxqSaleprice',width:'40%',editor:'textbox'">价格</th>
								<th data-options="field:'cpFunction',width:'10%',editor:'textbox'">主治功能</th>
							</tr>
						</thead>
					</table>
			<div><a id="drugtj" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">提交</a></div>	
			<div id="tbdrug" style="height:auto">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" id="drug-add">添加</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">移除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="accept()">应用</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
		</div>
        </div>
        </form>
        <div id="drugs" class="easyui-window" title="药品列表" data-options="collapsible:false,modal:true,
 																	minimizable:false,
																	maximizable:false,
 																	closed:true" style="width:50%; height:500px;background: rgb(236,243,255); padding:10px">
	<div id="ssNoFortitle">
	<span id="jianju"></span>
	<select id="mxjcptype" class="easyui-combobox" name="cpType" data-options="width:'7%',
											panelHeight:'auto',
											editable:false, 
											border:false,
											url:'AllDrugTypes',
											valueField:'cpType',
											textField:'cpType'">
											<option>全部</option>
											<option value="全部">全部</option>
											</select>
<input id="mxjcpname" class="easyui-textbox" data-options="width:'40%', 
											buttonText:'搜索',buttonIcon:'icon-search',
											prompt:'请输入药品名称'">																		
	</div>
	<table id="Mxjtbdrugs" class="easyui-datagrid" data-options="width:'100%',
																	fitColumns:true,fit:true,
																	pagination: true,
																	toolbar:'#ssNoFortitle',
																	singleSelect:true,
																	url:'AllDrug',
																	pageSize: 5,
																	pageList: [5, 10, 15, 20],
																	rownumbers: true,
																    border: false
																	">
					<thead>
						<tr>
						<th data-options="field:'cpNo',width:'10%'">编号</th>
						<th data-options="field:'cpName',width:'16%'">药品名称</th>
						<th data-options="field:'cpType',width:'16%'">类型</th>
						<th data-options="field:'cpNorms',width:'16%'">规格</th>
						<th data-options="field:'ygxqNumber',width:'16%'">数量</th>
						<th data-options="field:'yfxqUnit',width:'10%'">单位</th>
						<th data-options="field:'cpFunction',width:'16%'">主治功能</th>
						<th data-options="field:'ygxqSaleprice',width:'16%'">价格</th>
						</tr>
					</thead>
				</table>																						
	
</div>
</body>
</html>